package JHfstools;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;

public class hftssh {
	public static void jschsetup(){
		int g = 0;
		try {
			hftftp.logs = new FileOutputStream("ftplog.txt");
			hftftp.bls = new BufferedOutputStream(hftftp.logs);
			hftftp.pls = new PrintStream(hftftp.bls);
		} catch (FileNotFoundException e2) {
			e2.printStackTrace();
		}
		System.setOut(hftftp.pls);
		hftmain.logopen = true;
		JSch jsch;
		String UserName;
		String ConnectIP;
		int Port;
		String Password;
		Session ses;
		int TimeOut;
		jsch = new JSch();
		UserName = hftmain.d.getuid();
		Password = hftmain.d.getpswd();
		ConnectIP = hftmain.d.gettarg();
		Port = 22;
		TimeOut=60000;
		try {
			System.out.println("Known hosts is "+hftmain.d.getksf().toString());
			jsch.setKnownHosts(hftmain.d.getksf().toString());
			ses = jsch.getSession(UserName, ConnectIP, Port);
			ses.setPassword(Password);
			ses.connect(TimeOut);
			String rhn = ses.getHost();
			System.out.println(rhn);
			Channel channel = ses.openChannel("sftp");
			channel.connect();
			hftmain.hftstatus.setText("ssh connection established with "+ConnectIP);
			ChannelSftp c = (ChannelSftp) channel;
			String path = "/aiguser/zadsdth/";
			java.util.Vector vv=c.ls(path);
			for (int ii=0; ii < vv.size(); ii++){
			Object obj = vv.elementAt(ii);
			String dave = ((com.jcraft.jsch.ChannelSftp.LsEntry)obj).getFilename();
			System.out.println("File name is "+dave);
			SftpATTRS att = c.lstat(dave);
			System.out.println("Entry is a directory "+att.isDir());
			}
			ses.disconnect();
			hftmain.hftstatus.setText("ssh connection with "+ConnectIP+" disconnected.");
		} catch (JSchException e) {
			System.out.println(e.getMessage());
		} catch (SftpException e) {
			e.printStackTrace();
		}
	}
}
